//
//  MQSaveTableViewController.m
//  MQ
//
//  Created by 曾杰 on 15/10/28.
//  Copyright © 2015年 徐永祥. All rights reserved.
//

#import "MQSaveTableViewController.h"
#import "MQSavePicAndVideoTableViewCell.h"
#import "MQSaveReadCell.h"
#import "MQFMDBManager.h"
#import <FMDB.h>
#import "MQPictureInsterestingModel.h"
#import "MQReadCollectionModel.h"
@interface MQSaveTableViewController ()
@property(nonatomic, strong)NSMutableArray *dataArray;//数据数据数组

@end


@implementation MQSaveTableViewController
-(NSMutableArray *)dataArray
{
    if (_dataArray == nil) {
        self.dataArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"daohanglan"] forBarMetrics:UIBarMetricsDefault];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"全部清空" style:(UIBarButtonItemStylePlain) target:self action:@selector(clearAll:)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    [self.tableView registerClass:[MQSaveReadCell class] forCellReuseIdentifier:@"saveReadCell"];
    [self.tableView registerClass:[MQSavePicAndVideoTableViewCell class] forCellReuseIdentifier:@"savePicCell"];
    
    if (_Stag == 10)
    {
        [self selectPicDataBase];
    }
    else
    {
        [self selectReadDataBase];
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source


//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

//配置cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (_Stag == 11) {
        MQSaveReadCell *cell = [tableView dequeueReusableCellWithIdentifier:@"saveReadCell" forIndexPath:indexPath];
        MQReadCollectionModel *model = self.dataArray[indexPath.row];
        [cell cellWithModel:model];
         cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell.clearButton addTarget:self action:@selector(clearReadModel:) forControlEvents:(UIControlEventTouchUpInside)];
        return cell;
    }
    else
    {
        MQSavePicAndVideoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"savePicCell" forIndexPath:indexPath];
        MQPictureInsterestingModel *model = self.dataArray[indexPath.row];
        [cell cellWithModel:model];
         cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell.clearButton addTarget:self action:@selector(clearPic:) forControlEvents:(UIControlEventTouchUpInside)];
        return cell;
    }
   
    
}
//行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_Stag == 10) {
        MQPictureInsterestingModel *model = self.dataArray[indexPath.row];
        return [MQSavePicAndVideoTableViewCell HeightForModel:model];
    }else{
        MQReadCollectionModel *model = self.dataArray[indexPath.row];
        return [MQSaveReadCell HeightForModel:model];
    }
    
}

//右上角全部清除按钮
-(void)clearAll:(UIBarButtonItem *)barButton
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"全部清除成功" preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:alert animated:YES completion:nil];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self dismissViewControllerAnimated:YES completion:nil];
    });
    [self.dataArray removeAllObjects];
    [self.tableView reloadData];
    [[MQFMDBManager shareDataBase]executeUpdate:@"DELETE FROM PICTURES"];
    [[MQFMDBManager shareDataBase]executeUpdate:@"DELETE FROM READCOLLECTION"];
    [self sendNotifationToAll];
}
//删除图片收藏的某一行
-(void)clearPic:(UIButton *)button
{
    MQSavePicAndVideoTableViewCell *cell = (MQSavePicAndVideoTableViewCell *)button.superview.superview;
    NSIndexPath *index = [self.tableView indexPathForCell:cell];
    MQPictureInsterestingModel *model = self.dataArray[index.row];
    [[MQFMDBManager shareDataBase]executeUpdate:@"DELETE FROM PICTURES where wbody = ?",model.wbody];
    [self.dataArray removeObjectAtIndex:index.row];
    [self.tableView reloadData];
    [self sendNotifationToAll];
}
//查询数据库
// 删除阅读收藏的某一行
- (void)clearReadModel:(UIButton *)sender{
    MQSaveReadCell *cell = (MQSaveReadCell *)sender.superview.superview;
    NSIndexPath *index = [self.tableView indexPathForCell:cell];
    MQReadCollectionModel *model = self.dataArray[index.row];
    [[MQFMDBManager shareDataBase]executeUpdate:@"DELETE FROM READCOLLECTION where digest = ?",model.digest];
    [self.dataArray removeObjectAtIndex:index.row];
    [self.tableView reloadData];
    [self sendNotifationToAll];
}
// 查找图片收藏数据
- (void)selectPicDataBase
{
    FMResultSet *resultSet = [[MQFMDBManager shareDataBase] executeQuery:@"SELECT * FROM PICTURES"];
    while ([resultSet next]) {
        NSString *wbody = [resultSet stringForColumn:@"wbody"];
        NSString *wpic_middle = [resultSet stringForColumn:@"wpic_middle"];
        NSString *wpic_m_height = [resultSet stringForColumn:@"wpic_m_height"];
        NSString *islike = [resultSet stringForColumn:@"islike"];
        NSString *iszan = [resultSet stringForColumn:@"iszan"];
        MQPictureInsterestingModel *model = [[MQPictureInsterestingModel alloc]init];
        model.wbody = wbody;
        model.wpic_middle = wpic_middle;
        model.wpic_m_height = wpic_m_height;
        NSInteger isLike = [islike integerValue];
        BOOL like = (BOOL *)isLike;
        model.islike = like;
        NSInteger isZan = [iszan integerValue];
        BOOL zan = (BOOL *)isZan;
        model.iszan = zan;
        [self.dataArray addObject:model];
    }
    [resultSet close];
    [self.tableView reloadData];
}
// 查找阅读收藏数据
- (void)selectReadDataBase{
    FMResultSet *resultSet = [[MQFMDBManager shareDataBase] executeQuery:@"SELECT * FROM READCOLLECTION"];
    while ([resultSet next]) {
        NSString *digest = [resultSet stringForColumn:@"digest"];
        MQReadCollectionModel *model = [[MQReadCollectionModel alloc] init];
        model.digest = digest;
        [self.dataArray addObject:model];
    }
    [resultSet close];
    [self.tableView reloadData];
    
}
// 删除某些数据之后发出通知
- (void)sendNotifationToAll{
    NSDictionary *dic = [NSDictionary dictionaryWithObject:self.dataArray forKey:@"array"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"deleteCollection" object:nil userInfo:dic];
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
